/* -*-c++-*- */
/* osgEarth - Geospatial SDK for OpenSceneGraph
 * Copyright 2020 Pelican Mapping
 * http://osgearth.org
 *
 * osgEarth is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
 */

#ifndef OSGEARTH_GDALDEM_H
#define OSGEARTH_GDALDEM_H

#include <osgEarth/Common>
#include <osgEarth/ImageLayer>
#include <osgEarth/ElevationLayer>
#include <osgEarth/LayerReference>

namespace osgEarth
{
    /**
     * ImageLayer that exposes gdal's dem processing capabilities.
     */
    class OSGEARTH_EXPORT GDALDEMLayer : public ImageLayer
    {
    public: // serialization
        class OSGEARTH_EXPORT Options : public ImageLayer::Options {
        public:
            META_LayerOptions(osgEarth, Options, ImageLayer::Options);
            OE_OPTION_LAYER(ElevationLayer, elevationLayer);
            OE_OPTION(std::string, processing);
            OE_OPTION(float, azimuth);
            OE_OPTION(float, lightAltitude);
            OE_OPTION(bool, multidirectional);
            OE_OPTION(bool, combined);
            OE_OPTION(bool, alpha);
            OE_OPTION(URI, color_filename);
            virtual Config getConfig() const;
        private:
            void fromConfig(const Config& conf);
        };

    public:
        META_Layer(osgEarth, GDALDEMLayer, Options, ImageLayer, GDALDEMLayer);

    public:

        //! The elevation layer to get heightfields from.
        void setElevationLayer(ElevationLayer* layer);
        ElevationLayer* getElevationLayer() const;

        //! Processing option.  Depends on GDAL version; typical options: hillshade; slope; aspect; color-relief; TRI; TPI; roughness
        void setProcessing(const std::string& value);
        const std::string& getProcessing() const;

        //! Azimuth of the light, in degrees. 0 if it comes from the top of the raster, 90 from the east; typically 315; useful only for hillshade processing.
        void setAzimuth(const float& value);
        float getAzimuth() const;

        //! Altitude of the light, in degrees. 90 if the light comes from above the DEM, 0 if it is raking light; for hillshade processing
        void setLightAltitude(const float& value);
        float getLightAltitude() const;

        //! Multidirectional shading, a combination of hillshading illuminated from 225 deg, 270 deg, 315 deg, and 360 deg azimuth; useful only for hillshade processing.
        void setMultidirectional(const bool& value);
        bool getMultidirectional() const;

        //! Combined shading, a combination of slope and oblique shading; useful only for hillshade processing.
        void setCombined(const bool& value);
        bool getCombined() const;

        //! Color output supports alpha; useful only for color-relief processing.
        void setAlpha(const bool& value);
        bool getAlpha() const;

        //! Color text file containing lines of format "elevation_value red green blue [alpha]"; useful only for color-relief processing.
        void setColorFilename(const URI& value);
        const URI& getColorFilename() const;


    public: // Layer

        //! open succesfully
        virtual Status openImplementation();

        //! Close all composited layers
        virtual Status closeImplementation();

        //! Creates a raster image for the given tile key
        virtual GeoImage createImageImplementation(const TileKey& key, ProgressCallback* progress) const;

        //! Called when a layer is added to the map
        virtual void addedToMap(const Map* map);
        virtual void removedFromMap(const Map* map);

    protected: // Layer

        //! Called by constructors
        virtual void init();

    protected:

        //! Destructor
        virtual ~GDALDEMLayer() { }
    };

} // namespace osgEarth

OSGEARTH_SPECIALIZE_CONFIG(osgEarth::GDALDEMLayer::Options);


#endif // OSGEARTH_GDALDEM_H
